#include "statisticdistribution.h"

StatisticDistribution::StatisticDistribution(int type, long double seed) {
	
	if (seed==-1) {
		this->seed = this->generateSeed();
	} else {
		this->seed = seed;
	}
	
	this->urng = new MotherOfAll(this->seed);
	Random::Set(*urng);

	this->dist = NULL;
	switch (type) {
		case UNIFORM: 
			this->dist = new Uniform();
			break;
		case NORMAL: 
			this->dist = new Normal();
			break;
		case EXPONENTIAL: 
			this->dist = new Exponential();
			break;
		default: 
			this->dist = new Uniform();
			break;
	}
}

StatisticDistribution::~StatisticDistribution() {
	if (urng != NULL) delete urng;
	this->urng = NULL;
	if (this->dist!= NULL) delete dist;
	this->dist = NULL;
}

long double StatisticDistribution::generateSeed() {
	/* Structure to get the number for the seed */
	timeval tv;

	gettimeofday(&tv, NULL);

	/* getting the microsseconds passed since the start of the current second */
	long microseconds = (tv.tv_usec == 0) ? 1 : tv.tv_usec;

	/* Seed will have 999.999 different results */
	return  ( (long double) microseconds / 1000000 );
	
}

long int StatisticDistribution::next(long int lower, long int upper){
  return (long int)round((this->dist->Next() * (upper - lower)) + lower);
}

long double StatisticDistribution::next(long double lower, long double upper){
  return ((this->dist->Next() * (upper - lower)) + lower);
}

long double StatisticDistribution::getSeed() {
	return this->seed;
}
